package com.yuanchu.goods.controller;

import com.yuanchu.common.utils.R;
import com.yuanchu.common.validator.ValidationGroups;
import com.yuanchu.goods.dao.YoshopGoodsCategoryRelDao;
import com.yuanchu.goods.dto.EditGoodsCategoryDto;
import com.yuanchu.goods.dto.GoodsCategoryParamsDto;
import com.yuanchu.goods.entity.YoshopCategoryEntity;
import com.yuanchu.goods.service.YoshopCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 商品分类表
 *
 * @author adan
 * @email 2028038953@gmail.com
 * @date 2023-12-19 02:17:43
 */
@Api(value = "商品分类", tags = "商品分类controller")
@RestController
@RequestMapping("category")
public class YoshopCategoryController {

    private int storeId = 10001;

    @Autowired
    private YoshopCategoryService yoshopCategoryService;
    @Resource
    private YoshopGoodsCategoryRelDao goodsCategoryRelDao;

    /**
     * 列表
     */
    @ApiOperation("商品分类接口")
    // @PreAuthorize("hasAuthority('p2')")
    @GetMapping ("/list")
    public R list(@RequestParam(required = false) Map<String, Object> params) {
        List<YoshopCategoryEntity> list = yoshopCategoryService.queryGoodsCategory(params);
        List<YoshopCategoryEntity> collect = list.stream().filter(item -> item.getStatus() == 1).collect(Collectors.toList());
        return R.ok().put("data", collect);
    }

    @ApiOperation("商品分类接口")
    // @PreAuthorize("hasAuthority('p2')")
    @GetMapping ("/store/list")
    public R storeList(@RequestParam(required = false) Map<String, Object> params) {
        List<YoshopCategoryEntity> list = yoshopCategoryService.queryGoodsCategory(params);
        return R.ok().put("data", list);
    }

    @ApiOperation("添加商品分类")
    @PostMapping("/add")
    public R add(@RequestBody @Validated({ValidationGroups.Insert.class}) GoodsCategoryParamsDto form){
        yoshopCategoryService.addGoodsCategory(form, storeId);
        return R.ok();
    }

    @ApiOperation("修改商品分类")
    @PostMapping("/edit")
    public R edit(@RequestBody @Validated({ValidationGroups.Update.class}) EditGoodsCategoryDto goodsCategoryParams){
        yoshopCategoryService.editGoodsCategory(goodsCategoryParams, storeId);
        return R.ok();
    }
    @ApiOperation("删除商品分类")
    @PostMapping("/delete")
    public R delete(@RequestBody @Validated({ValidationGroups.Delete.class}) EditGoodsCategoryDto goodsCategoryParams){
        yoshopCategoryService.deleteGoodsCategoryById(goodsCategoryParams.getCategoryId(), storeId);
        return R.ok();
    }
    @ApiOperation("分类显示/隐藏")
    @PostMapping("/state")
    public R state(@RequestBody @Validated({ValidationGroups.Edit.class}) EditGoodsCategoryDto goodsCategoryParams) {
        boolean flag =  yoshopCategoryService.updateGoodsCategoryState(goodsCategoryParams);
        if (!flag){
            return R.error("操作失败");
        }
        return R.ok();
    }

    @ApiOperation("查询商品ids")
    @GetMapping("/goods/ids")
    public List<Integer> queryByGoodsIds(@RequestParam("categoryId") Integer categoryId){
        List<Integer> goodsIds = goodsCategoryRelDao.queryGoodsIdByCategoryId(categoryId);
        return goodsIds;
    }
}
